package com.proactive.webapp.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.ActivityDAO;
import com.proactive.webapp.dao.CardDAO;
import com.proactive.webapp.dao.CardTemplateDAO;
import com.proactive.webapp.dao.CourseCommentDAO;
import com.proactive.webapp.dao.CourseDAO;
import com.proactive.webapp.dao.CourseTagDAO;
import com.proactive.webapp.dao.DesignDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Activity;
import com.proactive.webapp.entity.Card;
import com.proactive.webapp.entity.CardTemplate;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.CourseComment;
import com.proactive.webapp.entity.CourseTag;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.DateConversion;
import com.proactive.webapp.util.FileUtil;
import com.proactive.webapp.util.HtmlUtil;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;

@Controller
public class CardController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	
	@RequestMapping(value = "/card", method = RequestMethod.GET)
	public ModelAndView getCardList(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();	
		
		CardDAO cardDAO = (CardDAO)context.getBean("cardDAO");
		ActivityDAO activityDAO = (ActivityDAO) context.getBean("activityDAO");
		MemberDAO memberDAO = (MemberDAO)context.getBean("memberDAO");
		
		//get data from request
		//int activityID = request.getParameter("aid")==null?1:Integer.parseInt(request.getParameter("aid"));
		int activityID = 1;
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		
		
		//set pager
		int pageSize = 30;
		int count = cardDAO.countTotal(activityID);
		String pageUrl = "card";
		String pageEndUrl1 = "&aid="+activityID;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		
		List<Card> cardList = new ArrayList<Card>();
		cardList = cardDAO.getList(activityID, pageCount.getStart(), pageSize);
		Activity activity = new Activity();
		activity.setId(activityID);
		activity.setStatus(1);
		activity = activityDAO.getByStatus(activity);
		
		List<Member> memberList = new ArrayList<Member>();
		memberList = memberDAO.getListTopByCard(0, 6);
		
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 5);

		model.addObject("memberList", memberList);
		model.addObject("cardList", cardList);
		model.addObject("activity", activity);
		model.addObject("page", pageCount);
		model.addObject("menuList", menuList);
		
		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"card1":"card2");
		
		return model;
	}
	
	@RequestMapping(value = "/showCard", method = RequestMethod.GET)
	public ModelAndView getCard(HttpServletRequest request, HttpSession session) {
		CardDAO cardDAO = (CardDAO) context.getBean("cardDAO");
		ActivityDAO activityDAO = (ActivityDAO) context.getBean("activityDAO");
		ModelAndView model = new ModelAndView();
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){

			//check login
			Member loginMember = (Member)session.getAttribute("loginMember");
			String loginAccount = loginMember!=null?loginMember.getAccount():"";
			
			int cardID = Integer.parseInt(request.getParameter("id"));
			
			Card card = new Card();
			card.setId(cardID);
			card = cardDAO.get(card);			
			
			Activity activity = new Activity();
			activity.setId(card.getAid());
			activity.setStatus(1);
			activity = activityDAO.getByStatus(activity);
			if(card.getTitle() != null){
				String shareImage = "";
				if (card.getTid() == 18) 
					shareImage = "resources/image/share/kp_img1.png";
				else if(card.getTid() == 19)
					shareImage = "resources/image/share/kp_img2.png";				
				else
					shareImage = activity.getShareImagePath();
				card.setWriterFlag(loginAccount.equals(card.getAccount())?true:false);
				
				List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 5);
				
				model.addObject("shareImage", shareImage);
				model.addObject("menuList", menuList);
				model.addObject("card", card);
				model.addObject("activity", activity);
				
				model.setViewName(loginMember!=null?"cardDetail1":"cardDetail2");
			}
			else
				model.setViewName("redirect:/error");
		}
		else
			model.setViewName("redirect:/card");
		return model;
	}
	
	@RequestMapping(value = "/cardHtml", method = RequestMethod.GET)
	public ModelAndView getCourse(HttpServletRequest request, HttpSession session) {
		CardDAO cardDAO = (CardDAO) context.getBean("cardDAO");
		CardTemplateDAO cardTemplateDAO = (CardTemplateDAO) context.getBean("cardTemplateDAO");
		ModelAndView model = new ModelAndView("cardHtml");
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){
			int cardID = Integer.parseInt(request.getParameter("id"));
			
			Card card = new Card();
			card.setId(cardID);
			card = cardDAO.get(card);
			card.setContent(card.getContent().replaceAll("(\r\n|\n)", "<br>").replaceAll(" ", "&nbsp;"));
			
			CardTemplate cardTemplate = new CardTemplate();
			cardTemplate.setId(card.getTid());
			cardTemplate.setStatus(1);
			cardTemplate = cardTemplateDAO.getByStatus(cardTemplate);
			
			String message = "";

			File file = new File(request.getServletContext().getRealPath(cardTemplate.getHtml2Path()));
			try {
				message = FileUtils.readFileToString(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			message = message.replace("example_img", "https://s3-ap-northeast-1.amazonaws.com/proactive/"+card.getImagePath());
			message = message.replace("example_text", card.getContent());
			model.addObject("message", message);
		}
		return model;
	}
}
